package cc.magicjson.example.demo.threadlocal.concurrent;

import cc.magicjson.example.demo.domain.UserInfo;
import cc.magicjson.example.demo.threadlocal.context.UserContext;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.task.TaskDecorator;

/**
 * {@link}
 *
 * @author <a href="mailto:it_magicjson@163.com">MagicJson</a>
 * @since 1.0.0
 */
public class UserTaskDecorator implements TaskDecorator {

	@NotNull
	@Override
	public Runnable decorate(@NotNull Runnable runnable) {
		UserInfo userInfo = UserContext.getUserInfo();
		return () -> {
			try {
				UserContext.setUserInfo(userInfo);
				runnable.run();
			}finally {
				UserContext.clearUserInfo();
			}
		};
	}
}
